import { mapActions, mapGetters } from 'vuex';
import { isCheckPhoneCode, sendTelCode } from '@/api';
import lang from '@/lang';

export default {
  data() {
    return {
      email: '',
      emailReg: /^[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?$/,
      phoneReg: /^1\d{10}$/,
      emailErr: false,
      emailErrText: '',
      password: '',
      passwordErr: false,
      passwordErrText: '',
      tel: '',
      time: 60,
      code: '',
      codeErr: false,
      codeErrText: '',
      gcode: '',
      gcodeErr: false,
      gcodeErrText: '',
      loading: false,
      show: 1,
      google_verify: 0,
      phone_verify: 0,
      timer: null,
    };
  },
  computed: {
    ...mapGetters(['subCuid', 'type']),
    telStar() {
      return this.tel.toString().replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    },
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {
    ...mapActions(['Login']),
    googleVerifyLostPrompt() {
      this.$alert(lang.t('login.login_info'), lang.t('login.login_info_two'), {
        confirmButtonText: lang.t('btn.confirm'),
        showClose: false,
        closeOnClickModal: true,
        callback: () => {},
      });
    },
    countdown() {
      this.timer = window.setInterval(() => {
        if (this.time === 1) {
          this.time = 60;
          window.clearInterval(this.timer);
          return;
        }
        this.time -= 1;
      }, 1000);
    },
    sendCode() {
      if (this.time !== 60) {
        return;
      }
      // 请求
      this.time -= 1;
      this.countdown();
      sendTelCode({
        tel: this.tel,
      }).then((res) => {
        if (res.data.status === '1') {
          this.$notify({
            message: res.data.msg,
            type: 'success',
            duration: 2000,
            showClose: false,
          });
        } else {
          this.$message.error(lang.t(`api.${res.data.errcode}`));
        }
      }).catch(() => {
        this.$message.error(lang.t('error.catchError'));
      });
    },
    login() {
      if (this.loading) {
        return;
      }
      if (!this.email) {
        this.emailErrText = lang.t('login.email_input');
        this.emailErr = true;
        return;
      }
      if (this.emailErr) {
        return;
      }
      if (!this.password) {
        this.passwordErrText = lang.t('login.pwd_input');
        this.passwordErr = true;
        return;
      }
      this.loading = true;
      isCheckPhoneCode({
        email: this.email,
        password: this.password,
      }).then((res) => {
        if (res.data.status === 1) {
          const data = res.data.data;
          this.google_verify = data.google_verify;
          this.phone_verify = data.phone_verify;
          if (data.phone_verify) {
            // 需开启验证
            this.tel = data.tel;
            this.show = 2;
            this.loading = false;
          }
          if (data.google_verify) {
            this.show = 3;
            this.loading = false;
          }
          if (!(data.phone_verify || data.google_verify)) {
            // 登录
            this.doLogin();
          }
        } else {
          this.loading = false;
          this.$message.error(lang.t(`api.${res.data.errcode}`));
        }
      }).catch(() => {
        this.loading = false;
        this.$message.error(lang.t('error.catchError'));
      });
    },
    doLogin(obj) {
      this.loading = true;
      this.Login(Object.assign({
        email: this.email,
        password: this.password,
      }, obj)).then(() => {
        this.loading = false;
        window.setTimeout(() => {
          if (this.subCuid) {
            if (this.$route.query.from_url) {
              this.$router.push(this.$route.query.from_url);
            } else {
              this.$router.push(this.type === 3 ? '/invite' : '/pools/dashboard');
            }
          } else {
            this.$router.push('/subaccount');
          }
        }, 1000);
      }).catch(() => {
        this.loading = false;
      });
    },
    verify() {
      if (this.loading) {
        return;
      }
      this.loading = true;
      if (this.show === 2) {
        if (!this.code) {
          this.codeErrText = lang.t('login.phone_code_input');
          this.codeErr = true;
          this.loading = false;
          return;
        }
        if (this.code.toString().length !== 6) {
          this.codeErrText = lang.t('login.phone_codegeshi_input');
          this.codeErr = true;
          this.loading = false;
          return;
        }
        this.doLogin({ code: this.code });
      } else if (this.show === 3) {
        if (!this.gcode) {
          this.gcodeErrText = lang.t('login.google_code_input');
          this.gcodeErr = true;
          this.loading = false;
          return;
        }
        if (this.gcode.toString().length !== 6) {
          this.gcodeErrText = lang.t('login.google_code_cofirm_input');
          this.gcodeErr = true;
          this.loading = false;
          return;
        }
        this.doLogin({ google_code: this.gcode });
      }
    },
  },
  watch: {
    email(v) {
      this.emailErrText = v ? lang.t('login.email_input_sure') : lang.t('login.email_input');
      this.emailErr = !this.emailReg.test(v) && !this.phoneReg.test(v);
    },
    password(v) {
      this.passwordErrText = v ? lang.t('login.pwd_input_sure') : lang.t('login.pwd_input');
      this.passwordErr = !v;
    },
    code(v) {
      this.code = v.toString().replace(/\D/g, '');
      this.codeErrText = this.code ? lang.t('login.phone_codegeshi_input') : lang.t('login.phone_code_input');
      this.codeErr = !this.code;
    },
    gcode(v) {
      this.gcode = v.toString().replace(/\D/g, '');
      this.gcodeErrText = this.gcode ? lang.t('login.google_code_cofirm_input') : lang.t('login.google_code_input');
      this.gcodeErr = !this.gcode;
    },
  },
};
